<!--电子围栏页-->
<template>
  <div class="view">
    <span class="view-title">围栏管理</span>
    <el-button @click="router.push('/geofenceMap')" class="title-right-button" type="primary">新建围栏</el-button>
  </div>
  <!-- 围栏查询卡片 -->
  <el-card class="search-card">
    <el-form :inline="true">
      <el-form-item label="围栏名称">
        <el-input placeholder="请输入" v-model="geoSearchForm.name" style="width:220px;"></el-input>
      </el-form-item>
      <el-form-item label="围栏状态" style="width:290px;">
        <el-select placeholder="请选择" v-model="geoSearchForm.status">
          <el-option label="启用" value="1"/>
          <el-option label="禁用" value="0"/>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="resetSearch">重置</el-button>
        <el-button @click="loadGeo" type="primary">查询</el-button>
      </el-form-item>
    </el-form>
  </el-card>
  <!-- 围栏表格数据 -->
  <el-card class="table-card">
    <el-table :data="geoTableData" stripe height="600">
      <el-table-column label="编号" prop="id" type="index" align="center" width="100"></el-table-column>
      <el-table-column label="围栏名称" prop="name" align="center"></el-table-column>
      <el-table-column label="围栏坐标点" prop="position" align="center" show-overflow-tooltip="true"
                       width="500"></el-table-column>
      <el-table-column label="创建时间" prop="createTime" align="center"></el-table-column>
      <el-table-column label="围栏状态" align="center">
        <template #default="scope">
          <el-switch
              inactive-text="启用"
              active-text="禁用"
              inline-prompt
              v-model="scope.row.status"
              inactive-value="0"
              active-value="1"
              @change="changeStatus(scope.row.id, scope.row.status)"
          ></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button type="info" @click="showGeofence(scope.row.id)" size="small">查看围栏</el-button>
          <el-button type="primary" @click="loadVehicle(scope.row.id)" size="small">管理车辆</el-button>
          <el-button type="danger" @click="handleDelete(scope.row.id)" size="small">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-card>
  <!-- 已绑定车辆弹窗 -->
  <el-dialog title="已绑定车辆" v-model="bindVehicleDialogVisible" style="padding:40px;">
    <el-button type="primary" @click="showUnbindVehicleList"
               style="margin:10px 0;">添加绑定车辆
    </el-button>
    <el-table :data="bindVehicleArr" height="450">
      <el-table-column prop="brand" label="车辆品牌" align="center"></el-table-column>
      <el-table-column prop="license" label="车牌号" align="center"></el-table-column>
      <el-table-column prop="type" label="车辆类型" align="center"
                       :formatter="vehicleTypeFormatter"></el-table-column>
      <el-table-column
          label="操作"
          fixed="right"
          width="200"
          align="center">
        <template #default="scope">
          <el-button link @click="removeBindVehicle(scope.row.id)" type="danger">移除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-dialog>
  <!-- 添加可绑定车辆列表弹窗 -->
  <el-dialog title="可绑定车辆列表" :before-close="handleCloseUnbindVehicleList"
             v-model="AddBindVehicleDialogVisible">
    <el-table :data="canBindVehicleList" height="450">
      <el-table-column label="编号" type="index" align="center" width="80px"></el-table-column>
      <el-table-column prop="brand" label="车辆品牌" align="center"></el-table-column>
      <el-table-column prop="license" label="车牌号" align="center"></el-table-column>
      <el-table-column prop="type" label="车辆类型" :formatter="vehicleTypeFormatter"
                       align="center"></el-table-column>
      <el-table-column prop="status" label="车辆状态" :formatter="vehicleStatusFormatter"
                       align="center"></el-table-column>
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button link @click="bindNewVehicle(scope.row.id)" type="primary">绑定</el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-dialog>
</template>
<script setup>
import {onMounted, ref} from "vue";
import axios from "axios";
import qs from "qs";
import {ElMessage, ElMessageBox} from "element-plus";
import router from "@/router";

//控制"已绑定车辆弹窗"是否显示
const bindVehicleDialogVisible = ref(false);
//控制"可绑定车辆列表"是否显示
const AddBindVehicleDialogVisible = ref(false);
//定义对象保存查询条件
const geoSearchForm = ref({name: '', status: ''});
//定义对象保存围栏表格数据
const geoTableData = ref([]);
//定义加载指定围栏车辆数据的方法
const bindVehicleArr = ref([]);
//查看围栏
const showGeofence = (geofenceId) => {
  axios.get(BASE_URL + '/v1/geofence/select?id=' + geofenceId)
      .then((response) => {
        if (response.data.code == 2000) {
          //跳转到geofence页面并传递参数值
          router.push({
            path:'/geofenceMap',
            query:{fenceData:response.data.data[0]}
          });
          // console.log(response.data.data[0]);
          // console.log(response.data.data[0].position.type);
          // if (response.data.data[0].position.type == 'circle'){
          //   alert('圆形围栏')
          // }else{
          //   alert('矩形围栏')
          // }
        } else {
          ElMessage.error(response.data.msg);
        }
      });
}
// 定义变量用来保存当前电子围栏的id,为了给后面围栏解绑时使用
var geoId;
//定义加载指定围栏车辆数据的方法
const loadVehicle = (geofenceId) => {
  geoId = geofenceId;
  bindVehicleDialogVisible.value = true;
  axios.get(BASE_URL + '/v1/vehicle/select?geofenceId=' + geofenceId)
      .then((response) => {
        if (response.data.code == 2000) {
          bindVehicleArr.value = response.data.data;
        } else {
          ElMessage.error(response.data.msg);
        }
      });
}
//对车辆状态显示内容进行文字转换
const vehicleStatusFormatter = (row, column, cellValue, index) => {
  if (cellValue == 1) {
    cellValue = '空闲';
  } else if (cellValue == 2) {
    cellValue = '占用';
  }
  return cellValue;
}
//对车辆类型显示内容进行文字转换
const vehicleTypeFormatter = (row, column, cellValue, index) => {
  if (cellValue == 10) {
    cellValue = '轿车';
  } else if (cellValue == 20) {
    cellValue = '货车';
  } else if (cellValue == 30) {
    cellValue = '客车';
  } else if (cellValue == 40) {
    cellValue = '挂车';
  } else if (cellValue == 50) {
    cellValue = 'SUV';
  } else if (cellValue == 60) {
    cellValue = 'MPV';
  }
  return cellValue;
}
//定义加载电子围栏的方法
const loadGeo = () => {
  let data = qs.stringify(geoSearchForm.value);
  axios.get(BASE_URL + '/v1/geofence/select?' + data)
      .then((response) => {
        if (response.data.code == 2000) {
          geoTableData.value = response.data.data;
        } else {
          ElMessage.error(response.data.msg);
        }
      });
}
onMounted(() => {
  loadGeo();
});
//定义重置方法
const resetSearch = () => {
  geoSearchForm.value = {}
}
//移除绑定状态
const removeBindVehicle = (vehicleId) => {
  axios.post(BASE_URL + '/v1/vehicle/unbind/' + vehicleId)
      .then((response) => {
        if (response.data.code == 2000) {
          ElMessage.success('操作成功!');
          loadVehicle(geoId);
        } else {
          ElMessage.error(response.data.msg);
        }
      });
}
//修改围栏状态
const changeStatus = (id, status) => {
  axios.post(BASE_URL + '/v1/geofence/update/status/' + id + '/' + status)
      .then((response) => {
        if (response.data.code == 2000) {
          ElMessage.success('操作成功!');
        } else {
          ElMessage.error(response.data.msg);
        }
      });
}
//定义变量存放可绑定车辆列表
const canBindVehicleList = ref([]);
//添加绑定车辆按钮
const showUnbindVehicleList = () => {
  AddBindVehicleDialogVisible.value = true;
  bindVehicleDialogVisible.value = false;
  loadCanBindVehicleList();
}
//加载可绑定车辆列表
const loadCanBindVehicleList = () => {
  axios.get(BASE_URL + '/v1/vehicle/select?geofenceBindStatus=0')
      .then((response) => {
        if (response.data.code == 2000) {
          canBindVehicleList.value = response.data.data;
        } else {
          ElMessage.error(response.data.msg);
        }
      });
}
// 关闭可绑定车辆列表的方法
const handleCloseUnbindVehicleList = () => {
  AddBindVehicleDialogVisible.value = false;
  bindVehicleDialogVisible.value = true;
  // 更新已绑定车辆列表
  loadVehicle(geoId);
}
//绑定车辆
const bindNewVehicle = (vehicleId) => {
  console.log(BASE_URL + '/v1/vehicle/bind/' + vehicleId + '/' + geoId);
  axios.post(BASE_URL + '/v1/vehicle/bind/' + vehicleId + '/' + geoId)
      .then((response) => {
        if (response.data.code == 2000) {
          ElMessage.success('操作成功!');
          // 更新可绑定车辆列表
          loadCanBindVehicleList();
        } else {
          ElMessage.error(response.data.msg);
        }
      });
}
//删除围栏
const handleDelete = (geofenceId) => {
  ElMessageBox.confirm(
      '删除后不可恢复,确定删除吗?',
      '警告',
      {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning',
      }
  )
      .then(() => {
        axios.post(BASE_URL + '/v1/geofence/delete/' + geofenceId)
            .then((response) => {
              if (response.data.code == 2000) {
                ElMessage.success('删除成功!');
                loadGeo();
              } else {
                ElMessage.error(response.data.msg);
              }
            });
      })
      .catch(() => {
        ElMessage({
          type: 'info',
          message: '操作取消',
        })
      })
}
</script>

<style scoped>
.view {
  height: 4vh;
  padding: 5px 20px;
  background-color: #fff;
}

.view-title {
  line-height: 40px;
  font-size: 20px;
  font-weight: bold;
}

.title-right-button {
  float: right;
  margin-top: 3px;
}

.dialog {
  width: 1000px;
  padding: 40px;
}

.search-card {
  margin: 20px;
  height: 70px;
}

.table-card {
  margin: 20px;
}
</style>
